import { message } from 'antd';

const BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080';

interface RequestOptions extends RequestInit {
  token?: string;
}

async function request<T>(endpoint: string, options: RequestOptions = {}): Promise<T> {
  const { token, ...restOptions } = options;
  const headers = {
    'Content-Type': 'application/json',
    ...(token && { Authorization: `Bearer ${token}` }),
    ...restOptions.headers,
  };

  try {
    const response = await fetch(`${BASE_URL}${endpoint}`, {
      ...restOptions,
      headers,
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.message || '请求失败');
    }

    return response.json();
  } catch (error) {
    message.error(error.message);
    throw error;
  }
}

export const api = {
  // 认证相关
  auth: {
    login: (data: { username: string; password: string }) =>
      request<{
        token: string;
        refreshToken: string;
        userType: string;
        username: string;
      }>('/api/auth/login', {
        method: 'POST',
        body: JSON.stringify(data),
      }),

    refresh: (data: { username: string; refreshToken: string }) =>
      request<{
        token: string;
        refreshToken: string;
      }>('/api/auth/refresh', {
        method: 'POST',
        body: JSON.stringify(data),
      }),
  },

  // 用户管理
  users: {
    list: (token: string) =>
      request<{
        items: Array<{
          id: number;
          username: string;
          email: string;
          phone: string;
          userType: string;
          enabled: boolean;
        }>;
        total: number;
      }>('/api/admin/users', {
        token,
      }),

    create: (data: {
      username: string;
      email: string;
      phone: string;
      userType: string;
      enabled: boolean;
    }, token: string) =>
      request('/api/admin/users', {
        method: 'POST',
        body: JSON.stringify(data),
        token,
      }),

    update: (id: number, data: {
      username: string;
      email: string;
      phone: string;
      userType: string;
      enabled: boolean;
    }, token: string) =>
      request(`/api/admin/users/${id}`, {
        method: 'PUT',
        body: JSON.stringify(data),
        token,
      }),

    delete: (id: number, token: string) =>
      request(`/api/admin/users/${id}`, {
        method: 'DELETE',
        token,
      }),
  },

  // 创作者管理
  creators: {
    list: (token: string) =>
      request<{
        items: Array<{
          id: number;
          username: string;
          realName: string;
          email: string;
          phone: string;
          level: number;
          rating: number;
          followers: number;
          verified: boolean;
          enabled: boolean;
        }>;
        total: number;
      }>('/api/admin/creators', {
        token,
      }),

    verify: (id: number, token: string) =>
      request(`/api/admin/creators/${id}/verify`, {
        method: 'POST',
        token,
      }),

    toggleStatus: (id: number, enabled: boolean, token: string) =>
      request(`/api/admin/creators/${id}/status`, {
        method: 'PUT',
        body: JSON.stringify({ enabled }),
        token,
      }),
  },

  // 系统设置
  settings: {
    get: (token: string) =>
      request<{
        siteName: string;
        siteDescription: string;
        contactEmail: string;
        contactPhone: string;
        enableRegistration: boolean;
        enableCreatorPlatform: boolean;
      }>('/api/admin/settings', {
        token,
      }),

    update: (data: {
      siteName: string;
      siteDescription: string;
      contactEmail: string;
      contactPhone: string;
      enableRegistration: boolean;
      enableCreatorPlatform: boolean;
    }, token: string) =>
      request('/api/admin/settings', {
        method: 'PUT',
        body: JSON.stringify(data),
        token,
      }),
  },
}; 